package com.jessechanrui.template.common.template.auth.websocket;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.handler.TextWebSocketHandler;

/**
 * @author chenrui
 * @version 1.0
 * @description ChatWebSocketHandlerTest
 * @date 2025/8/12
 */
@Slf4j
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
class ChatWebSocketHandlerClient {

    @Test
    public void chat() throws Exception {
        var client = new StandardWebSocketClient();

        var session = client.execute(new TextWebSocketHandler() {
            @Override
            protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
                log.info("handleTextMessage: {}", message.getPayload());
            }
        }, "ws://localhost:" + "8080" + "/ws/chat").get();
        ChatRequest request = new ChatRequest();
        request.setSessionId("session_10001");
        request.setContent("转账意图，比如：我要转账给张三；转账；我要给李四转账3000元；想要交房租；给王五生活费；给赵六零花钱；给我妈家用");

        session.sendMessage(new TextMessage(new ObjectMapper().writeValueAsString(request)));

        Thread.sleep(50 * 1000);
    }

}